package net.csdn.business.notify.api.action;

import lombok.extern.slf4j.Slf4j;
import net.csdn.business.notify.api.config.ApiConfigData;
import net.csdn.business.notify.common.enums.ProcessStatusEnum;
import net.csdn.business.notify.common.model.vo.NotifyResponseVO;
import net.csdn.business.notify.common.pipeline.BusinessProcess;
import net.csdn.business.notify.common.pipeline.ProcessContext;
import net.csdn.business.notify.common.pipeline.domain.PrivateTaskInfo;
import net.csdn.business.notify.common.pipeline.domain.SendPrivateTaskModel;
import net.csdn.business.notify.common.pipeline.param.MessageParam;
import net.csdn.business.notify.common.pipeline.param.SendPrivateParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * @ClassName SendPreCheckAction
 * @Description 发送消息：前置公共参数校验
 * @Author zhangyl
 * @Date 2023/11/29 16:49
 */
@Slf4j
@Service
public class PrivatePreCheckAction implements BusinessProcess<SendPrivateTaskModel> {

    @Autowired
    private ApiConfigData apiConfigData;

    @Override
    public void process(ProcessContext<SendPrivateTaskModel> context) {
        NotifyResponseVO response=context.getResponse();
        Map<String,String> failMap=response.getFail();
        SendPrivateTaskModel sendTaskModel = context.getProcessModel();
        List<MessageParam> messageParamList = sendTaskModel.getMessageParamList();
        if(messageParamList==null||messageParamList.size()==0){
            response.setStatus(ProcessStatusEnum.PARAMETERS_IS_NULL);
            context.setNeedBreak(true);
            return;
        }
        if(messageParamList.size() > apiConfigData.getBatchLimit()){
            response.setStatus(ProcessStatusEnum.EXCEED_BATCH_NUM_LIMIT);
            context.setNeedBreak(true);
            return;
        }
        //循环遍历组装taskInfo
        List<PrivateTaskInfo> taskInfos = new ArrayList<>();
        for (MessageParam messageParam:messageParamList){
            String batchNum= messageParam.getBatchNum();
            Set<String> receiver=messageParam.getReceiver();
            //1、接收者为空则，终止执行
            if(receiver==null||receiver.size()==0){
                failMap.put(batchNum,String.format(ProcessStatusEnum.CLIENT_BAD_PARAMETERS.getMsg(), "receiver"));
                continue;
            }
            // 2. 过滤 receiver 大于100的请求
            if(messageParamList.size() > apiConfigData.getBatchLimit()){
                failMap.put(batchNum,String.format(ProcessStatusEnum.EXCEED_RECEIVER_NUM_LIMIT.getMsg(), "receiver"));
                continue;
            }
            SendPrivateParam sendRemindParam =(SendPrivateParam)messageParam;
            PrivateTaskInfo privateTaskInfo=PrivateTaskInfo.builder()
                    .senderId(sendRemindParam.getSender())
                    .batchNum(sendRemindParam.getBatchNum())
                    .receiver(receiver)
                    .content(sendRemindParam.getContent())
                    .sendDate(new Date())
                    .startTime(context.getStartTime())
                    .build();
            taskInfos.add(privateTaskInfo);
        }
        if(taskInfos.size()==0){
            response.setStatus(ProcessStatusEnum.FAIL);
            context.setNeedBreak(true);
            return;
        }
        sendTaskModel.setTaskInfo(taskInfos);
    }
}
